package com.jiankang.askhealth.exception;

import com.jiankang.askhealth.core.api.base.ApiResult;
import com.jiankang.askhealth.util.ApiErrorMsgUtil;

public class ApiException extends Exception {

    private static final long serialVersionUID = -2169673265921983302L;

    private ApiResult result;

    public ApiException(int errorCode) {
        super(errorCode + "");
        String msg = ApiErrorMsgUtil.getErrorMsg(errorCode);
        result = new ApiResult(errorCode, msg);
    }

    public ApiException(int errorCode, Object[] args) {
        super(errorCode + "");
        String msg = ApiErrorMsgUtil.getErrorMsg(errorCode, args);
        result = new ApiResult(errorCode, msg);
    }

    public ApiException(Throwable e, int errorCode) {
        super(errorCode + "", e);
        String msg = ApiErrorMsgUtil.getErrorMsg(errorCode);
        result = new ApiResult(errorCode, msg);
    }

    public ApiException(Throwable e, int errorCode, Object[] args) {
        super(errorCode + "",e);
        String msg = ApiErrorMsgUtil.getErrorMsg(errorCode, args);
        result = new ApiResult(errorCode, msg);
    }

    public ApiException(Throwable e, int errorCode, String errorMsg) {
        super("[" + errorCode + "]" + errorMsg, e);
        result = new ApiResult(errorCode, errorMsg);
    }

    public ApiResult getResult() {
        return result;
    }

    @Override
    public String toString() {
        String errMsg = super.toString();
        if(result != null) {
            errMsg += ": " + result.getErrorMsg();
        }
        return errMsg;
    }
}

